<!DOCTYPE html>
<html>
    <head>
        <title>Pick-up Game</title>
        <link rel="stylesheet" href="style.css" type="text/css" />
        <link rel="stylesheet" href="friends.css" type="text/css" />
        <script type="text/javascript" src="friends.js"></script>


    </head>

    <body>
		
		  
        <?php include_once('menu.php'); ?>
        <?php
        session_start(); 
        if (!empty($_SESSION['username'])) { 
		// connect to database	
        $conn_str = 'host=dbsrv1.cdf.toronto.edu dbname=csc309g23 user=csc309g23 password=ipheid5h';
        $conn = pg_connect($conn_str);
        $query = 'SELECT * FROM members WHERE username=\'' . $_GET['postfriend'] . '\'';
        $friend_info_table = pg_query($conn, $query);
        $friend_info = pg_fetch_row($friend_info_table);
        ?>
        <div class="container">
        <div id="left_friends">		  
			<div style="border-bottom: 1px solid silver;" >
			  <img id="user_image" src="<?php echo $friend_info[7] ?>" align='middle' width=180 height=240 style="margin: 10px 10px 10px 10px;"/>							
			</div>	
		</div>
		<div id="right_friends">
		  <div id="user_info" >
			<table style="margin: 25px;" >
			  <?php if (($friend_info[15] == true) or ($friend_info[16] == true )) { ?>
				<tr> 	<td align="right" ><!-- <b>Name:      </b> --></td>	
					  <td><b style="font-size:150%;"> <?php
						if ($friend_info[15] == "t") {
						  echo $friend_info[1] . ' ' ; }
						if ($friend_info[16] == "t" ) {
						  echo $friend_info[2] . '</br>' ;} }?></b>
				</tr>
				<tr><td align="right" ><!-- <b>Name:      </b> --></td>	
				<td> <?php echo 'username: ' . $friend_info[0]; ?> </td> </tr>
							
				<?php if ($friend_info[12] == "t" and $friend_info[3])  {?>
					<tr> <td align="right" ><!--  <b>Birthday:      </b> --></td>
					  <td><?php 	echo $friend_info[3] . '</br>';  ?> </td>
					</tr> <?php } ?>	  
							
				<?php if ($friend_info[14] == "t" and $friend_info[4]) { ?>
					<tr> <td align="right" ><!-- <b>Address:      </b> --></td>
					  <td><?php echo $friend_info[4] . '</br>'; ?> </td>
					</tr> <?php } ?>
					
				<?php if ($friend_info[13] == "t" and $friend_info[17]) { ?>
					<tr> <td align="right" ><!-- <b>Email:      </b> --></td>
					  <td><?php echo $friend_info[17] . '</br>';  ?> </td>
					</tr> <?php } ?>						  
			</table>
		  </div>
		  
		  <div id="messages">
			<h2 align="center" style="margin: 10px 0px 10px 0px">Messages</h2>	
						  <p align="right">Write a message:</p>
                                <form action="sendMessage.php"  method="post" style="margin-left:85px;">
                                    <input id="new_ms" style="width:450px;" type="text" name="message" maxlength="256" /> 
                                    <input type="hidden" name="postfriend" value="<?= $_GET['postfriend'] ?>" />																													
                                    <input id="button" type="submit" value="Send"/>
                                </form>
                            <br/>
                            <?php							
							$message_query = 'SELECT wall.username, wall.postfriend, wall.postdate, wall.post, members.firstname, members.lastname, wall.postid ' .
											 'FROM wall LEFT JOIN members ON wall.username=members.username ' .
											 'WHERE (wall.postfriend=\'' . $_SESSION['username'] . '\' AND wall.username=\'' . $_GET['postfriend'] .
											 '\') OR (wall.username=\'' . $_SESSION['username'] . '\' AND wall.postfriend=\'' . $_GET['postfriend'] . '\') order by postdate desc';
							$friend_message = pg_query($conn, $message_query);
							
							while ($message = pg_fetch_row($friend_message)) {
								?>			
										<table width="100%">
										  <tr><td width="90%">
										  <div style="font-size:70%;">
											<strong style="color:#4682B4;font-size:110%;margin-left:5px;" ><?php echo $message[4] . ' ' . $message[5];?></strong>
											<p style="color:silver;float:right;"> <?	echo(strftime("%b %d, %Y %X %Z", strtotime($message[2])));  ?></p> 											
											<p style="margin-left:10px"><?= $message[3] ?></p>
										  </div> </td> 
										  <?php if ($message[0] == $_SESSION['username']) { ?>
											<td align="right">
											  <button type="button" onclick="location.href = 'deleteMessage.php?postID=<?=$message[6]?>';">Delete</button>
											</td>
											<?php } ?>
										  </tr>
										</table>	
							<?php } ?> <!-- end while -->
							
		  </div>
  </div>
<?php         
} else {
header('Location: /');
} ?>

    </body>
</html>
